
//
//  MYDynamicCommentReplyVC.m
//  MuYing
//
//  Created by tbb on 2019/7/24.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYDynamicCommentReplyVC.h"
#import "MYDynamicCommentCell.h"
@interface MYDynamicCommentReplyVC ()

@property (strong, nonatomic) NSMutableArray *replyArr;


@end

@implementation MYDynamicCommentReplyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = kYQLS(@"回复详情");

    self.page = 1;

    self.replyArr = NSMutableArray.new;
    [self getUserReplyListApi:YES];
    [self setupRefresh];
}

- (void)setupRefresh
{
    self.listTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getUserReplyListApi:YES];
    }];

    self.listTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (self.replyArr.count < 10) {
            [self.listTableView.mj_footer endRefreshing];
            return ;
        }
        self.page ++;

        [self getUserReplyListApi:NO];
    }];
    [self.listTableView.mj_header beginRefreshing];
    self.listTableView.mj_header.automaticallyChangeAlpha = YES;

}
#pragma amrk - POST列表
- (void)getUserReplyListApi:(BOOL)isReload
{
    if (isReload) {
        self.page = 1;
    }
    NSString *url = kPOST_COMMENT_REPLY_LIST_URL;


    NSMutableDictionary *dic = NSMutableDictionary.new;

    dic[@"page"] = @(self.page);
    dic[@"limit"] = @(10);
    dic[@"commentId"] = self.getModel.commentId;//@(self.getModel.commentId.intValue);
    if (self.isHot) {
        url = kPOST_INFORMATION_ARTICLE_COMMENT_LIST_URL;
        dic[@"informationId"] = self.invitationId;
    }
    
    [YQPNetWorkingManager sendPostRequest:url parameters:dic success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        if (self.page == 1) {
            [self.replyArr removeAllObjects];
        }
        YQPCommentListModel *model = [YQPCommentListModel mj_objectWithKeyValues:responseObject.data];
        [self.replyArr addObjectsFromArray:model.list];
        [self.listTableView reloadData];
        [self.listTableView.mj_header endRefreshing];
        [self.listTableView.mj_footer endRefreshing];
        if ([model.totalPage integerValue] == self.page) {
            [self.listTableView.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        [self.listTableView.mj_header endRefreshing];
        [self.listTableView.mj_footer endRefreshing];
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.replyArr.count;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    YQPCommentModel *model = nil;
    if (indexPath.row < self.replyArr.count) {
        model = self.replyArr[indexPath.row];
    }
    MYDynamicCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYDynamicCommentCell"];
    cell.commentBtn.hidden = YES;
    if (indexPath.row == 0) {
        cell.commentBtn.hidden = NO;
    }
    cell.model = model;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    YQPCommentModel *model = nil;
    if (indexPath.row < self.replyArr.count) {
        model = self.replyArr[indexPath.row];
    }
    return [tableView fd_heightForCellWithIdentifier:@"MYDynamicCommentCell" configuration:^(MYDynamicCommentCell *cell) {
        cell.model = model;
    }];
}

#pragma makr -- QMUITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        [self sendUserReplyApi:textView];
        return NO;
    }
    return YES;
}
#pragma mark - 发布评论
- (void)sendUserReplyApi:(UITextView *)textView
{
    NSMutableDictionary *dic = NSMutableDictionary.new;
    dic[@"content"] = textView.text;
    NSString *url = @"";
    if (self.isHot) {
        dic[@"commentId"] = self.getModel.commentId;
        dic[@"informationId"] = self.invitationId;
        url = kPOST_INFORMATION_ARTICLE_ADD_COMMENT_URL;
    }else{
        url = kPOST_TIE_LIST_COMMENT_URL;
        dic[@"invitationId"] = self.getModel.invitationId;
        dic[@"parentId"] = self.getModel.commentId;
    }

    [YQPNetWorkingManager sendPostRequest:url parameters:dic success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        if (responseObject.code == 0) {
            [MBProgressHUD showMessage:kYQLS(@"发布成功")];
            textView.text = @"";
            [self getUserReplyListApi:YES];
        }else{
            [MBProgressHUD showMessage:responseObject.msg];
        }

    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        [MBProgressHUD showMessage:error];
    }];
}
@end
